package test;

import java.util.Stack;

/**
 * @author : RobertWei
 * time: 2021/9/27    10:12
 * description:
 */

public class Demo {

    public static void main(String[] args) {

        String s = "()[[()";

        boolean valid = isValid(s);

        System.out.println("valid = " + valid);

    }


    public static boolean isValid(String s){

        Stack<Character> stack = new Stack<>();

        for (Character b : s.toCharArray()) {

            if (b == '('){
                stack.push(b);
            }
            if (b == ')'){
                if (stack.empty() || '(' != stack.pop()){
                    return false;
                }
            }

            if (b == '['){
                stack.push(b);
            }
            if (b == ']'){
                if (stack.empty() || '[' != stack.pop()){
                    return false;
                }
            }

            if (b == '{'){
                stack.push(b);
            }
            if (b == '}'){
                if (stack.empty() || '{' != stack.pop()){
                    return false;
                }
            }
        }
        return stack.empty();
    }

}

